package com.jyh.blog.controller;

import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.service.PageService;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.jyh.blog.constant.OptTypeConst.REMOVE;
import static com.jyh.blog.constant.OptTypeConst.SAVE_OR_UPDATE;

@Api(tags = "页面管理")
@RestController
public class PageController {
    @Autowired
    private PageService pageService;

    /**
     * 页面列表
     * @return
     */
    @ApiOperation(value = "页面列表")
    @GetMapping("admin/page")
    public Result<List<PageVO>> pageList(){
        return Result.ok(pageService.pageList());
    }

    /**
     * 添加或修改页面
     * @param pageVO
     * @return
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或修改页面")
    @PostMapping("admin/page")
    public Result saveOrUpdatePage(@Valid @RequestBody PageVO pageVO){
        pageService.saveOrUpdatePage(pageVO);
        return Result.ok();
    }

    /**
     * 通过id删除页面
     * @param id
     * @return
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "通过id删除页面")
    @DeleteMapping("admin/page/{id}")
    public Result deletePageById(@PathVariable("id") Integer id){
        pageService.deletePageById(id);
        return Result.ok();
    }
}
